﻿import QtQuick 2.12
import QtQuick.Window 2.12
import QtQuick.Controls 2.0
import Qt.labs.settings 1.0
import ImageItem 1.0
import "./utils" as Utils
import "./scraper" as Scraper


ApplicationWindow  {
    id: root
    width: 1280
    height: 800
    visible: true
    x:5400
    y:0
    flags: Qt.Window | Qt.FramelessWindowHint
    property var type : "未登录"
    property var host : "未登录"
    property var name : "未登录"
    property var model_
    // 使用 Settings 对象来持久化变量
    Settings {
        id: safetyInspectionSettings
        property bool safetyInspection: true // 默认值
    }
    header: Rectangle{
        id: headerRect
        width: parent.width
        height: 50
        color: "#1F2A37"
        Text {
            id: timeText
            anchors.verticalCenter: parent.verticalCenter
            anchors.left: parent.left
            anchors.leftMargin: 38
            color: Qt.rgba(255, 255, 255, 0.8)
            font{
                family: "Microsoft YaHei"
                pixelSize: 16
            }
            Timer {
                interval: 1000; running: true; repeat: true
                onTriggered: timeText.text = Qt.formatDateTime(new Date(), "yyyy-MM-dd hh:mm:ss ddd")
            }
        }

        Row{
            anchors.verticalCenter: parent.verticalCenter
            anchors.right: parent.right
            anchors.rightMargin: 30
            spacing: 30
            Text {
                anchors.verticalCenter: parent.verticalCenter
                text: type
                color: Qt.rgba(1, 1, 1, 0.8)
                font{
                    family: "Microsoft YaHei"
                    pixelSize: 16
                }
            }

            Row{
                spacing: 10
                Image{
                    source: "qrc:/imgs/user_icon.png"
                }
                Text {
                    anchors.verticalCenter: parent.verticalCenter
                    text: name
                    color: Qt.rgba(1, 1, 1, 0.8)
                    font{
                        family: "Microsoft YaHei"
                        pixelSize: 16
                    }
                }
            }

            Row{
                spacing: 10
                anchors.verticalCenter: parent.verticalCenter
                Image{
                    width: 40
                    height: 40
                    source: "qrc:/imgs/wifi.png"
                }
                Text {
                    id: pingText
                    color: "#00FF29"
                    width: 100
                    text: "未登录"
                    anchors.verticalCenter: parent.verticalCenter
                    font{
                        family: "Aldrich"
                        pixelSize: 16
                    }
                    Connections{
                        target: vehicle_manager
                        function onSignalVehiclePingTime(ping_time) {
                            pingText.text = parseFloat(ping_time).toFixed(1) + " ms";
                        }
                    }
                }
            }
            Image{
                height: 40
                width: 40
                source: "qrc:/imgs/close_8.png"
                MouseArea {
                    anchors.fill: parent
                    onClicked: {
                        myPopup.open()
                    }
                }
            }
        }
    }
    Utils.MyPopup {
        id:myPopup
        width:parent.width
        height: parent.height
        onClose_All: {
            // console.log("关机")
            can_manager.shutdownSystem()
        }
    }
    SwipeView {
        id: swipeView
        interactive: false
        anchors.fill: parent
        currentIndex: 0
        Rectangle {
            color: "#1F2A37"
            // 登录
            Utils.LoginPage{
                id: loginPage
                x:350
                y:80 
                onLoginSignal: {
                    image_manager.view_state = 0
                    swipeView.currentIndex = 1
                    swipeView.interactive = false
                    image_manager.vehicle_type = 0
                    vehicle_manager.connect_flag = true
                    root.name = model_[0]["name"]
                    vehicle_manager.current_vehicle_host = model_[0]["host"]
                    root.type = model_[0]["type"]   
                    // console.log(image_manager.vehicle_type,parent.width,parent.height)
                    can_manager.enableVehicleType(3)
                    // console.log("can_manager.turn_left_light: "+can_manager.turn_left_light)
                    // console.log("can_manager.turn_right_light: "+can_manager.turn_right_light)
                }
                Connections{
                    target: vehicle_manager
                    function onSignalVehiclesInfo(data){
                        model_ = JSON.parse(data)
                        ///console.log("qml - >"+data)
                    }
                }                
            }
            
            Component.onCompleted: {
                image_manager.view_state = -1
            }

        }

        Rectangle{
            color: "#1F2A37"
            Utils.CheckScenesWarnPage{
                anchors.fill: parent
                Timer{
                    id: logo_time
                    interval: 3000
                    onTriggered: {
                        logo_time.stop()
                        swipeView.currentIndex = 2
                        swipeView.interactive = false
                        image_manager.view_state = 1
                        image_manager.EnableSdk()
                        if(!safetyInspectionSettings.safetyInspection)
                        {
                            image_manager.view_state = 0;
                            swipeView.interactive = false
                            swipeView.currentIndex = 3 
                            swipeView.interactive = false
                            can_manager.runShellScript()
                        }
                    }
                }
                Connections{
                    target: swipeView
                    function onCurrentIndexChanged(){
                        // console.log(swipeView.currentIndex)
                        if(swipeView.currentIndex===1)
                        {
                            if(safetyInspectionSettings.safetyInspection)
                                logo_time.start()
                            else
                                logo_time.triggered();
                        }
                            
                    }
                }        
            }
        }

        // 车辆视野检查
        CheckScenesPage{
            id: checkScenesPage
            onStartDrive: {
                image_manager.view_state = 0;
                swipeView.interactive = false
                swipeView.currentIndex = 3 
                swipeView.interactive = false
                can_manager.runShellScript()
            }
            // onCurrentSceneIndexChanged: {
            //     image_manager.view_state = checkScenesPage.currentSceneIndex + 1
            // }
            
            Component.onCompleted: {
            }

        }
        // 车辆驾驶
        DrivePage{
            id: drivePage
            state: vehicle_manager.current_vehicle_type
            onBackIndex: {
                swipeView.currentIndex = 0
                swipeView.interactive = false
                can_manager.disenableVehicleType()
                image_manager.view_state = -1
                checkScenesPage.reClear()
                image_manager.DisableSdk()
                can_manager.stopShellScript()
            }
        }

        ClosePage{
            id: closePage
            onShotDownApp: {
                Qt.quit()
            }
        }
    }

    SubWindow{
        id: leftWindow
        minimumWidth:1080
        minimumHeight:1920
        maximumWidth:1080
        maximumHeight:1920
        imageIndex: 1
        imageCutNum: 5
        x: 0
        y:0
                //左边图像  
        ImageItem{
            id: leftImage
            anchors.fill: parent
            image_index: 1
            cut_num: 5
            visible: swipeView.currentIndex===3         
            Connections{
               target: image_manager
                function onSignalLeftImage(state, image){
                   leftImage.paintMainFiveSceenImage(0, image)
               }
            }
        }

        Scraper.ScraperLeftWindowItems{
            id: scraperLeftWindow
            anchors.fill: parent
        }      
        Connections{
            target: swipeView
            function onCurrentIndexChanged(){
                if(swipeView.currentIndex === 3)
                {
                    leftWindow.minimumWidth=1080
                    leftWindow.minimumHeight=730
                    leftWindow.maximumWidth=1080
                    leftWindow.maximumHeight=730
                    leftWindow.y=1190
                }else{
                    leftWindow.minimumWidth=1080
                    leftWindow.minimumHeight=1920
                    leftWindow.maximumWidth=1080
                    leftWindow.maximumHeight=1920
                    leftWindow.y=0
                }
                // console.log("leftWindow"+swipeView.currentIndex + "width:"+leftWindow.width+ "  height:"+leftWindow.height)
            }
        } 
        Component.onCompleted: {
            // console.log("width:"+leftWindow.width+ "  height:"+leftWindow.height)
        }
    }

    SubWindow{
        id: leftMidWindow
        minimumWidth:1080
        minimumHeight:1920
        maximumWidth:1080
        maximumHeight:1920
        imageIndex: 2
        imageCutNum: 5
        x: 1080
        y: 0 
        // 左中图像
        ImageItem{
            id: leftMidImage
            // anchors.fill: parent
            height:parent.height - 336
            width:parent.width
            // anchors.centerIn: parent
            anchors.top: parent.top
            anchors.topMargin: 136
            image_index: 2
            cut_num: 5
            visible: swipeView.currentIndex===3         
            Connections{
               target: image_manager
               function onSignalFrontImage(state, image){
                   leftMidImage.paintMainFiveSceenImage(1, image)
               }
            }
        }
        Scraper.ScraperLeftMidWindowItems{
            id: scraperLeftMidWindow
            anchors.fill: parent
        }
    }

    SubWindow{
        id: midWindow
        minimumWidth:1080
        minimumHeight:1920
        maximumWidth:1080
        maximumHeight:1920
        imageIndex: 3
        imageCutNum: 5
        x: 2160
        y: 0
        // 中间图像
        ImageItem{
            id: midImage
            // anchors.fill: parent
            height:parent.height - 336
            width:parent.width
            // anchors.centerIn: parent
            anchors.top: parent.top
            anchors.topMargin: 136
            image_index: 3
            cut_num: 5
            visible: swipeView.currentIndex===3         
            Connections{
               target: image_manager
               function onSignalFrontImage(state, image){
                   midImage.paintMainFiveSceenImage(1, image)
               }
            }
        }
     
        Scraper.ScraperMidWindowItems{
            id: scraperMidWindow
            anchors.fill: parent
        }
    }

    SubWindow{
        id: rightMidWindow
        minimumWidth:1080
        minimumHeight:1920
        maximumWidth:1080
        maximumHeight:1920
        imageIndex: 4
        imageCutNum: 5
        x: 3240
        y: 0
        // 右中图像
        ImageItem{
            id: rightMidImage
            // anchors.fill: parent
            height:parent.height - 336
            width:parent.width
            // anchors.centerIn: parent
            anchors.top: parent.top
            anchors.topMargin: 136
            image_index: 4
            cut_num: 5
            visible: swipeView.currentIndex===3         
            Connections{
               target: image_manager
               function onSignalFrontImage(state, image){
                   rightMidImage.paintMainFiveSceenImage(1, image)
               }
            }
        }
  
        Scraper.ScraperRightMidWindowItems{
            id: scraperRightMidWindow
            anchors.fill: parent
        }   
    }

    SubWindow{
        id: rightWindow
        minimumWidth:1080
        minimumHeight:1920
        maximumWidth:1080
        maximumHeight:1920
        imageIndex: 5
        imageCutNum: 5
        x: 4320
        y: 0
        // 右边图像
        ImageItem{
            id: rightImage
            anchors.fill: parent
            image_index: 5
            cut_num: 5
            visible: swipeView.currentIndex===3         
            Connections{
               target: image_manager
               function onSignalRightImage(state, image){
                   rightImage.paintMainFiveSceenImage(0, image)
               }
            }
        }
        Connections{
            target: swipeView
            function onCurrentIndexChanged(){
                if(swipeView.currentIndex === 3)
                {
                    rightWindow.minimumWidth=1080
                    rightWindow.minimumHeight=730
                    rightWindow.maximumWidth=1080
                    rightWindow.maximumHeight=730
                    rightWindow.y=1190
                }else{
                    rightWindow.minimumWidth=1080
                    rightWindow.minimumHeight=1920
                    rightWindow.maximumWidth=1080
                    rightWindow.maximumHeight=1920
                    rightWindow.y=0
                }
                // console.log("leftWindow"+swipeView.currentIndex + "width:"+leftWindow.width+ "  height:"+leftWindow.height)
            }
        } 
      
        Scraper.ScraperRightWindowItems{
            id: scraperRightWindow
            anchors.fill: parent
        }  

    }
    Window{
        id: backWindow
        x:2160
        y:1920
        flags: Qt.Window | Qt.FramelessWindowHint
        visible: true
        width: 1920
        height: 1080
        ImageItem{
            id: bottomImage
            anchors.fill: parent
            image_index: 6
            cut_num: 5
            visible: swipeView.currentIndex===3         
            Connections{
               target: image_manager
               function onSignalBackImage(state, image){
                   bottomImage.paintMainFiveSceenImage(0, image)
               }
            }
        }

        Scraper.ScraperBackWindowItems{
            id: scraperBackWindow
            anchors.fill: parent
        } 

    }
    Component.onCompleted: {
        vehicle_manager.start()
        can_manager.start()
        image_manager.start()
        image_manager.camera_changed = can_manager.camera_changed
    }
    Connections {
        target:can_manager
        function onCamera_changedChanged(){
            // console.log(can_manager.camera_changed)
            image_manager.camera_changed = can_manager.camera_changed
        }
    }
}






